<?php
/**
 * Created by PhpStorm.
 * User: Jmashimaro
 * Date: 2015/2/4
 * Time: 16:18 2015
 */
if (!array_key_exists('REQUEST_TIME_FLOAT', $_SERVER)) {
    $_SERVER ['REQUEST_TIME_FLOAT'] = microtime(1);
}

error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
date_default_timezone_set("Asia/Shanghai");
define('ROOT_PATH', dirname(__FILE__));  //根目录
require ROOT_PATH . '/eccore/ecmall.php';
require ROOT_PATH . '/app/common.app.php';
ecm_define(ROOT_PATH . '/data/config.inc.php'); //定义配置信息
set_error_handler('errorHandler');
set_exception_handler('exceptionHandler');
register_shutdown_function('shutdownHandler');
ECMall::startup(array(
    'default_app' => 'default',
    'default_act' => 'index',
    'app_root' => ROOT_PATH . '/app',
    'external_libs' => array(
        ROOT_PATH . '/includes/global.lib.php',
        ROOT_PATH . '/includes/libraries/time.lib.php',
        ROOT_PATH . '/includes/ecapp.base.php',
        ROOT_PATH . '/includes/plugin.base.php',
        ROOT_PATH . '/includes/frontend.base.php',
        ROOT_PATH . '/includes/subdomain.inc.php',
    ),
));
/**
 * @param array $error
 * @return null
 */
function errorReporting(array $error)
{
    $leves = array(
        '1' => 'E_ERROR',
        '2' => 'E_WARNING',
        '4' => 'E_PARSE',
        '8' => 'E_NOTICE',
        '16' => 'E_CORE_ERROR',
        '32' => 'E_CORE_WARNING',
        '64' => 'E_COMPILE_ERROR',
        '128' => 'E_COMPILE_WARNING',
        '256' => 'E_USER_ERROR',
        '512' => 'E_USER_WARNING',
        '1024' => 'E_USER_NOTICE',
        '2048' => 'E_STRICT',
        '4096' => 'E_RECOVERABLE_ERROR',
        '6143' => 'E_ALL',
        '8192' => 'E_DEPRECATED',
        '16384' => 'E_USER_DEPRECATED',
        '32767' => 'E_ALL',
    );
    //开启debug,不提交 E_NOTICE 级别的错误
    if ($error ['code'] == 8) return null;
    $error ['code'] = $leves [$error ['code']];
    $error['user_id'] = 0;
    $error['platform'] = 'ios';
    $error ['user_agent'] = $_SERVER ['HTTP_USER_AGENT'];
    $error ['current_url'] = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $error ['method'] = $_SERVER ['REQUEST_METHOD'];
    $error ['request_time'] = $_SERVER ['REQUEST_TIME'];
    if ($_POST) {
        foreach ($_POST as $key => $value) {
            $error ['current_url'] .= "&{$key}={$value}";
        }
    }
    $error ['response_time'] = time();
    try {
        $rpc = new Yar_Client(RPC_ERROR);
        $rpc->rec($error);
        header('Content-type: application/json');
        exit(json_encode(array('status' => FLAG_ERROR, 'message' => '网络连接失败,请稍后再试', 'data' => array())));
    } catch (Exception $e) {
        dd($e);
    }

}

function shutdownHandler()
{
    $error = error_get_last();

    if (!is_null($error)) {
        $error ['code'] = $error ['type'];
        errorReporting(array_merge($error, array('handler' => __FUNCTION__)));
    }
}

//
function exceptionHandler($e)
{
    if ($e instanceof Exception) {
        errorReporting(array(
            'line' => $e->getLine(),
            'code' => $e->getCode(),
            'file' => $e->getFile(),
            'message' => $e->getMessage(),
            'handler' => __FUNCTION__,
        ));
    }
}

function errorHandler($code, $message, $file, $line, array $context)
{
    errorReporting(array(
        'line' => $line,
        'code' => $code,
        'file' => $file,
        'message' => $message,
        'handler' => __FUNCTION__,
    ));
}

function dd()
{
    $temp = debug_backtrace();
    $trace = array('__FILE__' => $temp ['0']['file'], '__LINE__' => $temp ['0'] ['line']);
    echo '<pre>';
    array_map(function ($x) {
        print_r($x);
        echo PHP_EOL;
    }, $trace + func_get_args());
    die (1);
}

function ddd()
{
    $temp = debug_backtrace();
    $trace = array('__FILE__' => $temp ['0']['file'], '__LINE__' => $temp ['0'] ['line']);
    echo '<pre>';
    array_map(function ($x) {
        var_dump($x);
        echo PHP_EOL;
    }, $trace + func_get_args());
    die (1);
}

if (!function_exists('dump')) {
    function dump()
    {
        $temp = debug_backtrace();
        $trace = array('__FILE__' => $temp ['0']['file'], '__LINE__' => $temp ['0'] ['line']);
        echo '<pre>';
        array_map(function ($x) {
            print_r($x);
            echo PHP_EOL;
        }, $trace + func_get_args());
    }
}
/**
 * 获取数组部分内容
 *
 * @param array $datas
 * @param null|array 对应 data [ value ] 的值
 * @param null $key 对应 data [ key ] 的值
 * @param bool $multi 相同的key转换为多维还是直接覆盖
 * @return array
 */
function pluck(array $datas, $value = null, $key = null, $multi = false)
{
    $items = array();
    if (!is_array($datas)) return $items;
    foreach ($datas as $data) {
        if (is_null($value)) {
            $temp = $data;
        } elseif (is_array($value)) {
            $temp = array_intersect_key($data, array_flip($value));
        } else {
            $temp = $data [$value];
        }
        if (is_null($key)) {
            $items [] = $temp;
        } else {
            if ($multi === true) {
                $items [$data[$key]] [] = $temp;
            } else {
                $items [$data[$key]] = $temp;
            }
        }
    }
    return $items;
}